package com.zs.taskmanager.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zs.taskmanager.entity.Task;
import com.zs.taskmanager.repository.ITaskRepository;
import com.zs.taskmanager.service.ITaskService;

@Service
public class TaskServiceImpl implements ITaskService {
	@Resource
	private ITaskRepository taskRepository;

	@Override
	public String save(String taskName, String doTime, String user) {
		if (taskName != null && taskName.length() > 0) {
			Task findByNameAndUser = taskRepository.findByNameAndUser(taskName, user);
			if (findByNameAndUser != null) {
				return "error task ";
			} else {
				Task task = new Task();
				task.setName(taskName);
				task.setCreateTime(new Date());
				task.setUser(user);
				taskRepository.save(task);
				return "OK";
			}
		} else {
			return "error name is null";
		}

	}

	@Override
	public List<Task> findAll() {
		return this.taskRepository.findAll();
	}

	@Override
	public String delete(String id, String user) {
		this.taskRepository.deleteById(Long.valueOf(id));
		return "OK";
	}
}
